﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace LANmine
{
    public partial class PropertiesForm : Form
    {
        private Language lmLang;
        private String regDir = "HKEY_CURRENT_USER\\Software\\" + Application.ProductName;

        public PropertiesForm()
        {
            InitializeComponent();
        }

        public PropertiesForm(Language lmLang) : this()
        {
            // Save language object
            this.lmLang = lmLang;

            // Apply languages
            ApplyLanguage();

            // Fill up the controls
            FillValues();
        }

        #region Member Functions

        /// <summary>
        /// Display the currently set values in the NumericValues
        /// </summary>
        private void FillValues()
        {
            try 
            {
                // Get a RegistryKey object
                RegistryKey rKey = Registry.CurrentUser.OpenSubKey("Software\\" + Application.ProductName);

                // Check if the registry key exists
                if (rKey != null)
                {
                    // Fill in value boxes
                    numChatLog.Value = Convert.ToUInt16(rKey.GetValue("ChatLines", 100).ToString());
                    numActive.Value = Convert.ToUInt16(rKey.GetValue("TimeActiveUsers", 5).ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        
        /// <summary>
        /// Save the edited values to the registry
        /// </summary>
        private void EditValues()
        {
            try
            {
                // Put the proper values into the registry
                Registry.SetValue(regDir, "ChatLines", numChatLog.Value);
                Registry.SetValue(regDir, "TimeActiveUsers", numActive.Value);

                MessageBox.Show(lmLang.GetString("properties.msgSuccess")); //TODO RESOURCES
            }
            catch (Exception ex)
            {
                MessageBox.Show(lmLang.GetString("properties.msgFailed") + "\n" + ex.Message); //TODO resources

            }
        }

        #endregion

        #region Event Handlers

        private void btnSaveProperties_Click(object sender, EventArgs e)
        {
            EditValues();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite all text properties of all elements in all forms with the right ones.
         */
        private void ApplyLanguage()
        {
            // Header label
            this.lblProperties.Text = this.lmLang.GetString("properties.lblProperties");
            
            // Other labels & controls
            this.lblChatLines.Text = this.lmLang.GetString("properties.lblChatLines");
            this.lblTimeActiveUsers.Text = this.lmLang.GetString("properties.lblTimeActiveUsers");
            this.btnSaveProperties.Text = this.lmLang.GetString("properties.btnSaveProperties");
            this.btnClose.Text = this.lmLang.GetString("mteam.btnClose");
        }
        #endregion

    }
}
